package com.woniu.map.remote.url;

import com.woniu.internalcommon.constant.AmapConfigConstants;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.internalcommon.request.PointDTO;
import com.woniu.internalcommon.request.PointRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Service
public class YingYanUrl {

    @Value("${map.key}")
    private String mapKey;


    /**
     * 创建服务
     */
     public String buildAddYingYanService(String name){

         return  new StringBuilder().append(AmapConfigConstants.SERVICE_ADD_URL)
                 .append("?").append("key=").append(mapKey)
                 .append("&").append("name=").append(name)
                 .toString();
     }

    /**
     * 鹰眼 创建司机轨迹URL
     *
     * @param tid 轨迹id
     * @param sid
     * @return
     */

    public String buildAddTrack(String tid, String sid,String trname) {
        return UriComponentsBuilder.fromHttpUrl(AmapConfigConstants.TRACK_ADD)
                .queryParam("key", mapKey)
                .queryParam("sid", sid)
                .queryParam("tid", tid)
                .queryParam("trname",trname)
                .toUriString();
    }
    /**
     * 上传轨迹信息URl
     * @param pointRequest trid
     * @param sid 服务id
     */


    public String buildUploadTrack (PointRequest pointRequest,String sid){

        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.POINT_UPLOAD);
        url.append("?");
        url.append("key=").append(mapKey);
        url.append("&");
        url.append("sid=").append(sid);
        url.append("&");
        url.append("tid=").append(pointRequest.getTid());
        url.append("&");
        url.append("trid=").append(pointRequest.getTrid());
        url.append("&");
        url.append("points=");
        PointDTO[] points = pointRequest.getPoints();
        url.append("%5B");
      Arrays.stream(points).forEach(p -> url.append("%7B").append("%22location%22")
              .append("%3A")
              .append("%22").append(p.getLocation()).append("%22")
              .append("%2C")
              .append("%22locatetime%22")
              .append("%3A")
              .append(p.getLocatetime())
              .append("%7D"));
        url.append("%5D");
        return url.toString();
    }

    /**
     * 轨迹查询URL
     *
     */
    public String buildTraSearch(String tid, Long starttime , Long endtime,String sid){

        return   UriComponentsBuilder.fromHttpUrl(AmapConfigConstants.TERMINAL_TRSEARCH)
                .queryParam( "key", mapKey)
                .queryParam("sid",sid)
                .queryParam("tid",tid)
                .queryParam("trid",158)
                .queryParam("starttime",starttime)
                .queryParam( "endtime", endtime)
                .toUriString();
    }

    /**
     * 创建鹰眼终端url
     *
     * @param sid  服务id
     * @param name 终端名字
     * @param desc 终端详细信息
     * @return
     */

    public String buildAddTerminal(String sid, String name, String desc) {

        return new StringBuilder().append(AmapConfigConstants.TERMINAL_ADD)
                .append("?")
                .append("sid=").append(sid)
                .append("&")
                .append("name=").append(name)
                .append("&")
                .append("desc=").append(desc)
                .append("&")
                .append("key=").append(mapKey)
                .toString();
    }



//    {
//        "key": "k",
//            "sid": "1051214",
//            "page": "",
//            "name": "",
//            "tid": ""
//    }

    /**
     * 查询终端终端信息 指定id则是该id的信息，不指定则是该服务所有终端信息
     * @param sid
     * @param page
     * @param name
     * @param tid
     * @return
     */
    public  String buildGetTerminal(String sid, String page, String name, String tid) {

        return  new StringBuilder().append(AmapConfigConstants.TERMINAL_QUERY)
                .append("?")
                .append("key=").append(mapKey)
                .append("&")
                .append("sid=").append(sid)
                .append("&")
                .append("page=").append(page)
                .append("&")
                .append("name=").append(name)
                .append("&")
                .append("tid=").append(tid)
                .toString();
    }

    /** 查询周边终端信息
     *
     */
    public String buildAroundSearch(String sid ,String center, Integer radius) {

        return new StringBuilder().append(AmapConfigConstants.TERMINAL_AROUNDSEARCH)
                .append("?")
                .append("key=").append(mapKey)
                .append("&")
                .append("sid=").append(sid)
                .append("&")
                .append("center=").append(center)
                .append("&")
                .append("radius=").append(radius)
                .toString();


    }
}

